<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrapper {
            width: 400px;
            height: 80px;
            background-color: orange;
            position: absolute;
            left: -400px;
            top: 200px;
        }
        .wrapper span {
            width: 50px;
            height: 80px;
            background-color: red;
            position: absolute;
            right: -50px;
            top: 0px;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <span></span>
    </div>
    <script>
        var timer = null;
        var div = document.getElementsByClassName('wrapper')[0];

        div.onmouseover = function() {
            startMove(this, 0);
        }   
        div.onmouseout = function() {
            startMove(this, -400);
        }
        
        function startMove(dom, target) {
            clearInterval(timer);
            dom.style.left = window.getComputedStyle(dom, null).left;
            var speed = null;
            var num = 0;
            timer = setInterval(function(){
                speed = (target - parseInt(dom.style.left)) / 3;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                // 负数的向上取整是 -1.9 --> -1
                // Math.floor(-1.9) --> -2
                if(parseInt(dom.style.left) == target) {
                    clearInterval(timer);
                }else {
                    num = parseInt(dom.style.left) + speed;
                    // console.log(num);
                    dom.style.left = num  + 'px'; 
                }
            }, 30);
        }
    </script>
</body>
</html>